package com.panshi.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.panshi.modules.domain.SysRole;
import com.panshi.modules.mapper.SysRoleMapper;
import com.panshi.modules.service.SysRoleService;
import com.panshi.vo.SelectVO;
import com.panshi.vo.SysRoleVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {


    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRoleVO> listSysRolesByUserId(Integer id) {

        List<SysRole> sysRoles = sysRoleMapper.listSysRolesByUserId(id);

        if (CollectionUtils.isEmpty(sysRoles)) {
            return new ArrayList<>();
        }
        List<SysRoleVO> list = new ArrayList<>();
        for(SysRole role : sysRoles){

            SysRoleVO roleVO = new SysRoleVO();
            BeanUtils.copyProperties(role,roleVO);
            list.add(roleVO);
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    public IPage<SysRole> pageRoleList(IPage<SysRole> page){
        return sysRoleMapper.selectPage(page, null);
    }

    @Override
    public List<SysRoleVO> listSysRole() {
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        List<SysRoleVO> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(sysRoles)) {
            return list;
        }
        for(SysRole role : sysRoles){
            SysRoleVO roleVO = new SysRoleVO();
            BeanUtils.copyProperties(role,roleVO);
            list.add(roleVO);
        }
        return list;
    }

    @Override
    public SysRoleVO getSysRole(Integer id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        SysRoleVO sysRoleVO = new SysRoleVO();
        if(sysRole != null){
            BeanUtils.copyProperties(sysRole,sysRoleVO);
        }
        return sysRoleVO;
    }

    @Override
    public int addSysRole(SysRoleVO sysRoleVO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleVO,sysRole);
        int result = sysRoleMapper.insert(sysRole);

        String menuIds = sysRoleVO.getMenuIds();
        if(StringUtils.isNotBlank(menuIds)){
            List<String> listStr = Arrays.asList(sysRoleVO.getMenuIds().split(","));
            //角色-菜单关系表维护
            sysRoleMapper.addRoleMenus(sysRole.getId(), listStr);
        }
        return result;
    }

    @Override
    public int editSysRole(SysRoleVO sysRoleVO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleVO,sysRole);
        int result = sysRoleMapper.updateById(sysRole);

        //角色-菜单关系表维护
       // List<Integer> integers = sysRoleMapper.selectRoleMenuRelas(sysRoleVO.getId());
        String menuIds = sysRoleVO.getMenuIds();

        if(StringUtils.isNotBlank(menuIds)){
            //删除旧的关系表
            sysRoleMapper.delRoleMenusByRoleId(sysRoleVO.getId());
            //写入新的关系表
            List<String> listMenuIds = Arrays.asList(menuIds.split(","));
            sysRoleMapper.addRoleMenus(sysRoleVO.getId(), listMenuIds);
        }

        return result;
    }

    @Override
    public int delSysRole(Integer id) {
        int result = sysRoleMapper.deleteById(id);

        int del = sysRoleMapper.delRoleMenusByRoleId(id);
        return result;
    }

    @Override
    public int countRolename(String rolename,Integer roleId) {
        return sysRoleMapper.selectCount(new QueryWrapper<SysRole>().eq("rolename",rolename).ne("id",roleId));
    }


    @Override
    public List<SelectVO> selectRoleList() {
        List<SysRoleVO> list = this.listSysRole();
        List<SelectVO> collect = list.stream().map(sysRoleVO -> new SelectVO(sysRoleVO.getRolename(), sysRoleVO.getId().toString())).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SelectVO> getRoleSelectByUserId(Integer id) {
        List<SysRoleVO> list = this.listSysRolesByUserId(id);
        List<SelectVO> collect = list.stream().map(sysRoleVO -> new SelectVO(sysRoleVO.getRolename(), sysRoleVO.getId().toString())).collect(Collectors.toList());
        return collect;
    }
}
